﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace RAPP
{
    public partial class StatMain : Form
    {
        public StatMain()
        {
            InitializeComponent();
        }
        #region 公用变量
        private int count;
        private int hour;
        private bool tl = false;
        private int ss;
        private string path = Application.StartupPath + @"\age.txt";
        private Times times;
        #endregion

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            label1.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH点mm分ss秒");
            if (tl)
            {
                ss += 1;
                if (ss >= 10)
                {
                    label11.Visible = false;
                    ss = 0;
                }
                return;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            times = new Times(Convert.ToInt32(comboBox1.SelectedValue),
                              Convert.ToInt32(comboBox2.SelectedValue),
                              Convert.ToInt32(comboBox3.SelectedValue));
            InitTime();
            label10.Text = CountTiem(times.Years, times.Months, times.Days);
            label9.Text = CountTiem(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        }

        #region 方法集

        private void InitTime()
        {
            label1.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH点mm分ss秒");
            DataBind(comboBox1, GetYearSelectList());
            DataBind(comboBox2, GetMonthSelectList());
            DataBind(comboBox3, GetDaySelectList(Convert.ToInt32(comboBox1.SelectedValue),
               Convert.ToInt32(comboBox2.SelectedValue)));
            comboBox1.SelectedValue = DateTime.Now.Year.ToString();
            comboBox2.SelectedValue = DateTime.Now.Month.ToString();
            comboBox3.SelectedValue = DateTime.Now.Day.ToString();
            times = new Times(Convert.ToInt32(comboBox1.SelectedValue),
                              Convert.ToInt32(comboBox2.SelectedValue),
                              Convert.ToInt32(comboBox3.SelectedValue));
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            this.timer1.Start();
        }

        private string CountTiem(int y, int m, int d)
        {
            int[] bz = new int[8];
            bz[0] = Math.Abs((y - 3) % 10);
            bz[1] = (y - 3) % 12;
            bz[2] = bz[0] * 2 + m > 10 ? (bz[0] * 2 + m)%10 : bz[0] * 2 + m;
            //m--; 
            bz[3] = m+2>=12?12-m:m+2;
            int n=m%2==0?30:0;
                 if (m == 1 || m == 4 || m == 5){n += 1;}
            else if (m == 2 || 6 == 4 || 7 == 5){n += 2;}
            else if (m == 8) { n += 3; }
            else if (m == 9 || m == 10) { n += 4; }
            else if (m == 11 || m == 12) { n += 5; }
            bz[4] = (y % 100 * 5 + y % 100 / 4 + 9 + d+n) % 60;
            bool result = (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
            if (m ==1||m==2)
            {
                bz[4] = result ? bz[4] -= 1 : bz[4];
            }
            bz[5] = bz[4] % 12;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 6; i++)
            {
                if ((i + 1) % 2 != 0)
                {
                    sb.Append(Enum.GetName(typeof(TD.Tian), bz[i]));
                    continue;
                }
                sb.Append(Enum.GetName(typeof(TD.DI), bz[i]) + "-");
            }
            label12.Text = Enum.GetName(typeof(TD.SX), bz[1]);
            return sb.ToString();
        }
        private void CombS(object sender, EventArgs e)
        {
            DataBind(comboBox3, GetDaySelectList(Convert.ToInt32(comboBox1.SelectedValue),
              Convert.ToInt32(comboBox2.SelectedValue)));
        }
        private void DataBind(ComboBox cb, IEnumerable<SelectListItem> ie)
        {
            cb.DataSource = new List<SelectListItem>(ie);
            cb.ValueMember = "Value";
            cb.DisplayMember = "Text";
            cb.SelectedIndexChanged += new EventHandler(Age);
            if (cb.Name == "comboBox3") return;
            cb.SelectedIndexChanged += new EventHandler(CombS);
        }

        private void Age(object sender, EventArgs e)
        {
            times = new Times(Convert.ToInt32(comboBox1.SelectedValue),
                              Convert.ToInt32(comboBox2.SelectedValue),
                              Convert.ToInt32(comboBox3.SelectedValue));
            int year = DateTime.Now.Year - Convert.ToInt32(comboBox1.SelectedValue);
            string time = comboBox1.SelectedValue + "-" + comboBox2.SelectedValue + "-" + comboBox3.SelectedValue;
            DateTime dt1 = Convert.ToDateTime(time);
            DateTime dt2 = DateTime.Now;
            if ((dt2 - dt1).TotalDays > 0 && (dt2 - dt1).TotalDays < 1)
                count = 0;
            else
                count=Convert.ToInt32((dt2 - dt1).TotalDays);
            hour = count == 0 ? DateTime.Now.Hour : count * 24 + DateTime.Now.Hour;
            int Month = (dt2.Year - dt1.Year) * 12 + (dt2.Month - dt1.Month);
            label3.Text = "你的诞生年数：" + year + "年";
            label4.Text = "你的诞生天数：" + count + "天";
            label6.Text = "你的诞生时数：" + hour + "时";
            label5.Text = "你的诞生秒数：" + hour * 60 * 60 + DateTime.Now.Second + "秒";
            label7.Text = "你的诞生月数：" + Month + "月";
            label8.Text = "你的诞生分数：" + hour * 60 + DateTime.Now.Minute + "分";
            label10.Text = CountTiem(times.Years, times.Months, times.Days);
            if (dt1 > dt2) timer2.Stop(); else timer2.Start();
        }

        #endregion

        private void timer2_Tick(object sender, EventArgs e)
        {
            label5.Text = "你的诞生秒数：" + hour * 60 * 60 + DateTime.Now.Second + "秒";
            label8.Text = "你的诞生分数：" + hour * 60 + DateTime.Now.Minute + "分";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (FileStream fs = new FileStream(path,
                FileMode.OpenOrCreate, FileAccess.Write))
            {
                StreamWriter sr = new StreamWriter(fs);
                StringBuilder sb = new StringBuilder();
                sb.Append(comboBox1.SelectedValue.ToString());
                sb.Append("-" + comboBox2.SelectedValue.ToString());
                sb.Append("-" + comboBox3.SelectedValue.ToString());
                sr.WriteLine(sb);
                sr.Close();
                fs.Close();
            }
            label11.Visible = true;
            tl = true;
            label11.Text = "保存成功！";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string timer;
            using (FileStream fs = new FileStream(path,
                FileMode.Open, FileAccess.Read))
            {
                StreamReader sr = new StreamReader(fs);
                timer = sr.ReadLine();
                string[] timers = timer.Split('-');
                comboBox1.SelectedValue = timers[0];
                comboBox2.SelectedValue = timers[1];
                comboBox3.SelectedValue = timers[2];
            }
            label11.Visible = true;
            tl = true;
            label11.Text = "读取成功！";
        }

        private IEnumerable<SelectListItem> GetYearSelectList()
        {
            for (var i = 1900; i <= 3000; i++)
            {
                yield return new SelectListItem { Text = i.ToString() + "年", Value = i.ToString() };
            }
        }
        private IEnumerable<SelectListItem> GetMonthSelectList()
        {
            for (var i = 1; i <= 12; i++)
            {
                yield return new SelectListItem { Text = i.ToString() + "月", Value = i.ToString() };
            }
        }
        private IEnumerable<SelectListItem> GetDaySelectList(int year, int month)
        {
            var anyDate = new DateTime(year, month, 1);
            var lastDayOfMonth = anyDate.AddMonths(1).AddDays(-1).Day; //该月最后一天
            for (var d = 1; d <= lastDayOfMonth; d++)
            {
                yield return new SelectListItem { Text = d.ToString() + "号", Value = d.ToString() };
            }
        }

        private void tbtnBz_Click(object sender, EventArgs e)
        {
            //FormDia.Excute();
        }
    }

    class SelectListItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }
    class Times
    {
        public Times(int y, int m, int d)
        {
            Years = y;
            Months = m;
            Days = d;
        }
        public Times(int y, int m, int d, int h)
        {
            Years = y;
            Months = m;
            Days = d;
            Hours = h;
        }
        public int Years { get; set; }
        public int Months { get; set; }
        public int Days { get; set; }
        public int Hours { get; set; }

    }

    class Bazu{
        public int Ty { get; set; }
        public int Dy { get; set; }
        public int Tm { get; set; }
        public int Dm { get; set; }
        public int Td { get; set; }
        public int Dd { get; set; }
        public int Tr { get; set; }
        public int Dr { get; set; }
    }
}
